package pdfreader;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;

public class IOUtility {
	
	public String GetTextFromTXT(String fileName){
//        File file = new File("res/"+fileName+".txt");
		File file = new File(fileName);
        BufferedReader in;
        String text = "";
        try {
            in = new BufferedReader(new FileReader(file));
            String line;
            try {
                while((line = in.readLine()) != null){
                    text = text + line;
                }
                in.close();
            } catch (NumberFormatException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  //
        return text;
    }
    
	public String getTextFromPDF(String pdfFilePath) {  
        String result = null;  
        FileInputStream is = null;  
        PDDocument document = null;  
        try {  
            is = new FileInputStream(pdfFilePath);  
            PDFParser parser = new PDFParser(is);  
            parser.parse();  
            document = parser.getPDDocument();  
            PDFTextStripper stripper = new PDFTextStripper();  
            result = stripper.getText(document);  
        } catch (FileNotFoundException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } finally {  
            if (is != null) {  
                try {  
                    is.close();  
                } catch (IOException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
            }  
            if (document != null) {  
                try {  
                    document.close();  
                } catch (IOException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
            }  
        }  
        return result;  
    }  
    
    public String getText(String filePah) throws IOException{
    	File file = new File(filePah);
    	FileInputStream fis = new FileInputStream(file);     
        PDFParser p = new PDFParser(fis);     
        p.parse();
        PDDocument pdd = p.getPDDocument();
//    	PDDocument pdd = PDDocument.load(file);
        PDFTextStripper ts = new PDFTextStripper();     
        String c = ts.getText(pdd);     
        pdd.close();
        fis.close();
        return c;
    }

    public static List<String> getFileName(String folder) {
    	File f = new File(folder);
    	List<String> pathList = new ArrayList<String>();
    	if (!f.exists()) {
    		System.out.println(folder + "path not exists");
    		return pathList;
    	}
    	File fa[] = f.listFiles();
    	for (int i = 0; i < fa.length; i++) {
    		File fs = fa[i];
    		if (fs.isDirectory()) {
    			System.out.println(fs.getName() + " [Directory]");
    		} else {
//    			System.out.println(fs.getName());
    			pathList.add(fs.getName());
    		}
    	}
    	return pathList;
 	}
    
}
